﻿namespace QuickMail.BusinessLogic
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net.Mail;
    using System.Security.Cryptography;
    using System.Windows.Media.Imaging;

    using HigLabo.Net.Smtp;

    using QuickMail.DataModels;
    using QuickMail.Properties;

    /// <summary>
    /// Contains helper methods.
    /// </summary>
    static class HelpWorker
    {
        /// <summary>
        /// Checks the correctness of input email address.
        /// </summary>
        public static bool IsValidMail(string value)
        {
            if (String.IsNullOrEmpty(value))
                return false;

            try
            {
                // ReSharper disable once ObjectCreationAsStatement
                new MailAddress(value);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Divides the string into an array of words.
        /// </summary>
        public static List<string> DivideWords(string words)
        {
            return (from email in words.Split(new[] { ',', ';' })
                    where !String.IsNullOrEmpty(email.Trim())
                    select email.Trim()).ToList();
        }

        /// <summary>
        /// Combines array of words into string.
        /// </summary>
        public static string CombineWords(List<string> fieldList)
        {
            string resultField = String.Empty;

            if (fieldList == null)
                return resultField;

            var index = 0;
            foreach (var field in fieldList)
            {
                if (fieldList.Count() - 1 == index)
                {
                    resultField += field;
                }
                else
                {
                    resultField += field + ", ";
                }

                index++;
            }

            return resultField;
        }

        /// <summary>
        /// Computes the hash of the file.
        /// </summary>
        /// <param name="filePath">Path to the file.</param>
        /// <returns>md5 hash</returns>
        public static string ComputeHash(string filePath)
        {
            using (var md5 = MD5.Create())
            {
                using (var stream = File.OpenRead(filePath))
                {
                    return md5.ComputeHash(stream).ToString();
                }
            }
        }

        public static BitmapImage GetFileImage(string extention)
        {
            switch (extention.ToLower())
            {
                case ".jpg":
                case ".jpeg":
                case ".png":
                case ".bmp":
                case ".psd":
                case ".tif":
                case ".tiff":
                    return new BitmapImage(new Uri("pack://application:,,,/QuickMail;component/Resources/image_file-48w.png"));

                case ".rar":
                case ".7z":
                case ".7zip":
                case ".zip":
                case ".tar":
                case ".bz2":
                    return new BitmapImage(new Uri("pack://application:,,,/QuickMail;component/Resources/archive2-48w.png"));

                case ".txt":
                case ".bat":
                case ".cmd":
                case ".xml":
                    return new BitmapImage(new Uri("pack://application:,,,/QuickMail;component/Resources/document-48w.png"));

                case ".pdf":
                    return new BitmapImage(new Uri("pack://application:,,,/QuickMail;component/Resources/pdf-48w.png"));

                case ".doc":
                case ".docx":
                    return new BitmapImage(new Uri("pack://application:,,,/QuickMail;component/Resources/word_logo-48w.png"));
                case ".xls":
                case ".xlsx":
                    return new BitmapImage(new Uri("pack://application:,,,/QuickMail;component/Resources/excel_logo-48w.png"));
                case ".ppt":
                case ".pptx":
                    return new BitmapImage(new Uri("pack://application:,,,/QuickMail;component/Resources/powerpoint_logo-48w.png"));
                case ".pub":
                    return new BitmapImage(new Uri("pack://application:,,,/QuickMail;component/Resources/publisher-48w.png"));

                case ".mp3":
                case ".wav":
                case ".wma":
                case ".aac":
                case ".ac3":
                case ".midi":
                    return new BitmapImage(new Uri("pack://application:,,,/QuickMail;component/Resources/audio_file-48w.png"));

                case ".avi":
                case ".mkv":
                case ".flv":
                case ".ts":
                case ".mpg":
                case ".wmv":
                case ".mp4":
                    return new BitmapImage(new Uri("pack://application:,,,/QuickMail;component/Resources/video_file-48w.png"));
            }

            return new BitmapImage(new Uri("pack://application:,,,/QuickMail;component/Resources/file-48w.png"));
        }

        /// <summary>
        /// Return type of encrypted connection
        /// </summary>
        public static SmtpEncryptedCommunication GetCryptType()
        {
            switch ((CryptTypes)Settings.Default.SendCryptType)
            {
                case CryptTypes.None:
                    return SmtpEncryptedCommunication.None;
                case CryptTypes.Ssl:
                    return SmtpEncryptedCommunication.Ssl;
                case CryptTypes.Tls:
                    return SmtpEncryptedCommunication.Tls;
            }

            return SmtpEncryptedCommunication.None;
        }

        /// <summary>
        /// Return current authentication mode
        /// </summary>
        public static SmtpAuthenticateMode GetAuthenticateMode()
        {
            switch ((AuthenticateTypes)Settings.Default.SendAutinfecationType)
            {
                case AuthenticateTypes.Auto:
                    return SmtpAuthenticateMode.Auto;
                case AuthenticateTypes.Cram_MD5:
                    return SmtpAuthenticateMode.Cram_MD5;
                case AuthenticateTypes.Login:
                    return SmtpAuthenticateMode.Login;
                case AuthenticateTypes.Plain:
                    return SmtpAuthenticateMode.Plain;
                case AuthenticateTypes.PopBeforeSmtp:
                    return SmtpAuthenticateMode.PopBeforeSmtp;
                case AuthenticateTypes.None:
                    return SmtpAuthenticateMode.None;
            }

            return SmtpAuthenticateMode.Auto;
        }

        /// <summary>
        /// Checks whether there is a folder and file and
        /// loads the program language from XML file.
        /// </summary>
        public static LanguageXml GetLocalisation(string path)
        {
            //_xmlWorker.CheckingLangFile(AppDomain.CurrentDomain.BaseDirectory + @"Languages\English.xml");
            //_xmlWorker.SerializeLang(AppDomain.CurrentDomain.BaseDirectory + @"Languages\English.xml", new LanguageXml());

            if (string.IsNullOrEmpty(path))
                path = AppDomain.CurrentDomain.BaseDirectory + @"Languages\English.xml";

            string pathDir = Path.GetDirectoryName(path);
            if (string.IsNullOrEmpty(pathDir))
                return new LanguageXml();

            if (!Directory.Exists(pathDir))
            {
                Directory.CreateDirectory(pathDir);
                XmlWorker.SerializeLang(path, new LanguageXml());
                return new LanguageXml();
            }
            
            if (File.Exists(path))
            {
                return XmlWorker.DeserializeLang(path);
            }
            //else
            {
                XmlWorker.SerializeLang(path, new LanguageXml());
                return new LanguageXml();
            }
        }
    }
}
